<%@ include file="header.jsp" %>
<%
	if (!user.hasPrivilege("jwsm.service_manage")) {
		response.sendRedirect("index.jsp?error=Insufficient privileges to modify services");
	}

	com.googlecode.jwsm.Service service = com.googlecode.jwsm.ServiceManager.getInstance().getService(request.getParameter("service"));
%>
<iframe id="uploadIframe" name="uploadIframe" src="" style="width: 1px; height: 1px; border: 0;">
</iframe>
<span id="error" style="color: ff0000;"></span>
<form id="serviceForm" method="post" action="Uploader" target="uploadIframe" enctype="multipart/form-data">
<input type="hidden" name="serviceType" value="<%if (service != null) {%>update<%} else {%>create<%}%>"/>
<table cellpadding="10" cellspacing="0" class="table">
	<tr>
		<td align="center" bgcolor="<%= CSSDefaults.BG_COLOR %>" colspan="2"><span style="color: white; font-size: 14pt; font-weight: bold;"><%
			if (service != null) {
		%>Configure<%
			} else {
		%>Deploy<%
			}
		%> Service</span></td>
	</tr>
	<tr>
		<td align="right" id="DeployName"><b>Name:</b></td>
		<td><input type="text" id="name" name="name"/></td>
	</tr>
	<tr valign="top">
		<td align="right" id="JarList"><b>JARs:</b></td>
		<td>
			<span id="jars">
				<span id="jarSpan0">
					<input type="file" id="jar0" name="jar0" onchange="changeJar();"/><input type="button" id="RemoveJAR" value="Remove" onclick="removeJar(0);"/><br/>
				</span>
			</span>
			<br/>
			<input id="DependencyDuplication" type="button" value="Dependency Duplication Check" onclick="uploadState = 'dependencyDuplication'; validateUploads();"/>
		</td>
	</tr>
	<tr>
		<td align="right" id="mainClassTD"><b>Main Class:</b></td>
		<td><input type="text" id="mainClass" name="mainClass"/> <input type="button" id="ChooseMainClass" value="Choose" onclick="uploadState = 'mainClass'; validateUploads();"/></td>
	</tr>
	<tr>
		<td align="right" id="MethodDefault"><b>Default Method:</b></td>
		<td><select id="defaultMethod" name="defaultMethod"/><option value="">&lt;no default method&gt;</option></select></td>
	</tr>
	<tr valign="top">
		<td align="right" id="MethodsAvailable"><b>Available Methods:</b></td>
		<td><span id="availableMethods"><i>No Class Selected</i></span></td>
	</tr>
	<tr valign="top">
		<td align="right" id="CustomInstantiation"><b>Custom Instantiation:</b></td>
		<td><textarea cols="40" rows="5" name="instantiation" id="instantiation"></textarea>
	</tr>
	<tr valign="top">
		<td align="right" id="Advanced"><b>Advanced:</b></td>
		<td>
			<span id="InstantiatePerInvocation"><input type="checkbox" name="requestInstantiation" id="requestInstantiation"/> Instantiate per invocation</span><br/>
			<span id="ModeDebug"><input type="checkbox" name="debugMode" id="debugMode"/> Debug Mode</span><br/>
			<span id="WSDLIntercept"><input type="checkbox" name="interceptWSDL" id="interceptWSDL" checked="checked"/> Intercept WSDL</span><br/>
		</td>
	</tr>
	<tr>
		<td align="center" colspan="2"><input type="button" value="<%if (service != null) {%>Update<%} else {%>Create<%}%>" onclick="validateSubmit();"/></td>
	</tr>
</table>
</form>
<%
	if (user.hasPrivilege("jwsm.wsar_manage")) {
%>
<br/><br/>
<form method="post" action="WebServiceArchive" enctype="multipart/form-data">
<table cellpadding="10" cellspacing="0" class="table">
	<tr>
		<td align="center" bgcolor="<%= CSSDefaults.BG_COLOR %>" colspan="2"><span style="color: white; font-size: 14pt; font-weight: bold;">Upload WSAR</span></td>
	</tr>
	<tr>
		<td align="right" id="WSARFile"><b>WSAR:</b></td>
		<td><input type="file" id="wsar" name="wsar"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2" id="UploadWSAR"><input type="submit" value="Upload"/></td>
	</tr>
</table>
</form>
<%
	}
%>
<%
	if (request.getParameter("service") != null) {
		java.io.File directory = new java.io.File(com.googlecode.jwsm.Uploader.directory, service.getName());
		java.io.File[] jars = directory.listFiles(com.googlecode.jwsm.Service.jarFilter);
%><script language="JavaScript"><%// Service Name%>document.getElementById('name').value = '<%=service.getName()%>';<%%>document.getElementById('name').setAttribute('readonly', 'readonly');<%// Add JARs
		for (int i = 0; i < jars.length; i++) {%>
				//document.getElementById('jars').appendChild(document.createElement('br'));
				var jarSpan = document.createElement('span');
				jarSpan.setAttribute('id', 'jarSpan<%=i%>');
				document.getElementById('jars').appendChild(jarSpan);
				var span = document.getElementById('jarSpan<%=i%>');
				span.innerHTML = '<input type="text" name="jar<%=i%>" id="jar<%=i%>" value="<%=jars[i].getName()%>" readonly="readonly"/><input type="hidden" name="fileName<%=i%>" id="fileName<%=i%>" value="<%=service.getName() + "/" + jars[i].getName()%>"/><input type="button" value="Remove" onclick="removeJar(<%=i%>);"/><br/>';
			<%}%>changeJar();<%// Main Class
		if (service.getMainClass() != null) {%>setMainClass('<%=service.getMainClass().getName()%>');<%}
		
		// Default Methods
		try {
			int selectedIndex = 0;
			java.util.ArrayList methodArray = com.googlecode.jwsm.Service.getValidMethods(service.getMainClass());%>clearMethods();<%
			for (int i = 0; i < methodArray.size(); i++) {
				String outgoingXSLT = null;
				if (((java.lang.reflect.Method)methodArray.get(i)).getReturnType() != void.class) {
					outgoingXSLT = "'" + service.getOutgoingXSLTMapping(((java.lang.reflect.Method)methodArray.get(i)).getName()) + "'";
				}
				
				String incomingXSLT = null;
				if (((java.lang.reflect.Method)methodArray.get(i)).getParameterTypes().length > 0) {
					incomingXSLT = "'" + service.getIncomingXSLTMapping(((java.lang.reflect.Method)methodArray.get(i)).getName()) + "'";
				}
				
				%>addMethod('<%= ((java.lang.reflect.Method)methodArray.get(i)).getName() %>', <%= outgoingXSLT %>, <%= incomingXSLT %>);<%
				%>document.getElementById('<%= ((java.lang.reflect.Method)methodArray.get(i)).getName() %>').checked = false;<%
				if (((java.lang.reflect.Method)methodArray.get(i)).getName().equals(service.getDefaultMethod())) {
					selectedIndex = i + 1;
				}
			}
			
			java.lang.reflect.Method[] methods = service.getMethods();
			for (int i = 0; i < methods.length; i++) {
				%>document.getElementById('<%= methods[i].getName() %>').checked = true;<%
			}
			%>document.getElementById('defaultMethod').selectedIndex = <%= selectedIndex %>;<%
		} catch(Throwable t) {
			%>document.getElementById('error').innerHTML = 'Missing Class: <%= t.getMessage().replace('/', '.') %>';<%
		}
		
		if (service.getInstantiation() != null) {
			%>document.getElementById('instantiation').value = '<%= service.getInstantiation() %>';<%
		}
		
		// Advanced
		%>document.getElementById('requestInstantiation').checked = <%= service.isInstanceInstantiation() %>;<%
		%>document.getElementById('debugMode').checked = <%= service.isDebug() %>;<%
		%>document.getElementById('interceptWSDL').checked = <%= service.isInterceptWSDL() %>;<%
		
		%></script><%
	}
%>
<%@ include file="footer.jsp" %>